package com.cws.project.cws.score.service.impl;

import java.util.List;

import com.cws.common.utils.DateUtils;
import com.cws.common.utils.text.Convert;
import com.cws.project.cws.homework.domain.Homework;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cws.project.cws.score.mapper.HomeworkScoreMapper;
import com.cws.project.cws.score.domain.HomeworkScore;
import com.cws.project.cws.score.service.IHomeworkScoreService;

/**
 * 作业评分Service业务层处理
 *
 * @author xyl
 * @date 2022-01-12
 */
@Service
public class HomeworkScoreServiceImpl implements IHomeworkScoreService {
    @Autowired
    private HomeworkScoreMapper homeworkScoreMapper;

    /**
     * 查询作业评分
     *
     * @param scoreId 作业评分ID
     * @return 作业评分
     */
    @Override
    public HomeworkScore selectHomeworkScoreById(Long scoreId) {
        return homeworkScoreMapper.selectHomeworkScoreById(scoreId);
    }

    /**
     * 查询作业评分列表
     *
     * @param homeworkScore 作业评分
     * @return 作业评分
     */
    @Override
    public List<HomeworkScore> selectHomeworkScoreList(HomeworkScore homeworkScore) {
        return homeworkScoreMapper.selectHomeworkScoreList(homeworkScore);
    }

    /**
     * 新增作业评分
     *
     * @param homeworkScore 作业评分
     * @return 结果
     */
    @Override
    public int insertHomeworkScore(HomeworkScore homeworkScore) {
        homeworkScore.setCreateTime(DateUtils.getNowDate());
        return homeworkScoreMapper.insertHomeworkScore(homeworkScore);
    }

    /**
     * 修改作业评分
     *
     * @param homeworkScore 作业评分
     * @return 结果
     */
    @Override
    public int updateHomeworkScore(HomeworkScore homeworkScore) {
        homeworkScore.setUpdateTime(DateUtils.getNowDate());
        return homeworkScoreMapper.updateHomeworkScore(homeworkScore);
    }

    /**
     * 删除作业评分对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHomeworkScoreByIds(String ids) {
        return homeworkScoreMapper.deleteHomeworkScoreByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除作业评分信息
     *
     * @param scoreId 作业评分ID
     * @return 结果
     */
    @Override
    public int deleteHomeworkScoreById(Long scoreId) {
        return homeworkScoreMapper.deleteHomeworkScoreById(scoreId);
    }

    /**
     * 学生提交作业
     * @param homeworkScore
     * @return
     */
    @Override
    public int submitHomework(HomeworkScore homeworkScore) {
        HomeworkScore hs = homeworkScoreMapper.selectHomeworkIsExist(homeworkScore.getUserId(), homeworkScore.getHomeworkId());
        if(ObjectUtils.isNotEmpty(hs)){
            homeworkScore.setScoreId(hs.getScoreId());
            homeworkScore.setUpdateTime(DateUtils.getNowDate());
            return homeworkScoreMapper.updateHomeworkScore(homeworkScore);
        }else{
            homeworkScore.setCreateTime(DateUtils.getNowDate());
            return homeworkScoreMapper.insertHomeworkScore(homeworkScore);
        }
    }
}
